// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class OrderMapper
    {

        /// <summary />
        static OrderMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Order> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.OrderSelectAll;

            return Load(commands, dataMappers);
        }

        private Order.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Order.DataTransfer data=new Order.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Order.DataTransfer FillData(Order.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("OrderID");
            if (!record.IsDBNull(ord))
                data.OrderID=record.GetInt32(ord);

            ord=record.GetOrdinal("CustomerID");
            if (!record.IsDBNull(ord))
                data.CustomerID=record.GetString(ord);

            ord=record.GetOrdinal("EmployeeID");
            if (!record.IsDBNull(ord))
                data.EmployeeID=record.GetInt32(ord);

            ord=record.GetOrdinal("ShipVia");
            if (!record.IsDBNull(ord))
                data.ShipVia=record.GetInt32(ord);

            ord=record.GetOrdinal("OrderDate");
            if (!record.IsDBNull(ord))
                data.OrderDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("RequiredDate");
            if (!record.IsDBNull(ord))
                data.RequiredDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("ShippedDate");
            if (!record.IsDBNull(ord))
                data.ShippedDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("Freight");
            if (!record.IsDBNull(ord))
                data.Freight=record.GetDecimal(ord);

            ord=record.GetOrdinal("ShipName");
            if (!record.IsDBNull(ord))
                data.ShipName=record.GetString(ord);

            ord=record.GetOrdinal("ShipAddress");
            if (!record.IsDBNull(ord))
                data.ShipAddress=record.GetString(ord);

            ord=record.GetOrdinal("ShipCity");
            if (!record.IsDBNull(ord))
                data.ShipCity=record.GetString(ord);

            ord=record.GetOrdinal("ShipRegion");
            if (!record.IsDBNull(ord))
                data.ShipRegion=record.GetString(ord);

            ord=record.GetOrdinal("ShipPostalCode");
            if (!record.IsDBNull(ord))
                data.ShipPostalCode=record.GetString(ord);

            ord=record.GetOrdinal("ShipCountry");
            if (!record.IsDBNull(ord))
                data.ShipCountry=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Order.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderSelect,
                parameterBuilder.GetMarker("OrderID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderDelete,
                parameterBuilder.GetMarker("OrderID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.OrderID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderInsert,
                parameterBuilder.GetMarker("OrderID", 1),
                parameterBuilder.GetMarker("CustomerID", 2),
                parameterBuilder.GetMarker("EmployeeID", 3),
                parameterBuilder.GetMarker("ShipVia", 4),
                parameterBuilder.GetMarker("OrderDate", 5),
                parameterBuilder.GetMarker("RequiredDate", 6),
                parameterBuilder.GetMarker("ShippedDate", 7),
                parameterBuilder.GetMarker("Freight", 8),
                parameterBuilder.GetMarker("ShipName", 9),
                parameterBuilder.GetMarker("ShipAddress", 10),
                parameterBuilder.GetMarker("ShipCity", 11),
                parameterBuilder.GetMarker("ShipRegion", 12),
                parameterBuilder.GetMarker("ShipPostalCode", 13),
                parameterBuilder.GetMarker("ShipCountry", 14)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.OrderID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CustomerID", 2);
            p.DbType=DbType.String;
            p.Value=(object)data.CustomerID ?? (object)DBNull.Value;
            p.Size=5;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("EmployeeID", 3);
            p.DbType=DbType.Int32;
            p.Value=data.EmployeeID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipVia", 4);
            p.DbType=DbType.Int32;
            p.Value=data.ShipVia;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderDate", 5);
            p.DbType=DbType.Date;
            p.Value=data.OrderDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("RequiredDate", 6);
            p.DbType=DbType.Date;
            p.Value=data.RequiredDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShippedDate", 7);
            p.DbType=DbType.Date;
            p.Value=data.ShippedDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Freight", 8);
            p.DbType=DbType.Decimal;
            p.Value=data.Freight;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipName", 9);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipName ?? (object)DBNull.Value;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipAddress", 10);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipAddress ?? (object)DBNull.Value;
            p.Size=60;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipCity", 11);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipCity ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipRegion", 12);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipRegion ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipPostalCode", 13);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipPostalCode ?? (object)DBNull.Value;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipCountry", 14);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipCountry ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Order.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderUpdate,
                parameterBuilder.GetMarker("CustomerID", 1),
                parameterBuilder.GetMarker("EmployeeID", 2),
                parameterBuilder.GetMarker("ShipVia", 3),
                parameterBuilder.GetMarker("OrderDate", 4),
                parameterBuilder.GetMarker("RequiredDate", 5),
                parameterBuilder.GetMarker("ShippedDate", 6),
                parameterBuilder.GetMarker("Freight", 7),
                parameterBuilder.GetMarker("ShipName", 8),
                parameterBuilder.GetMarker("ShipAddress", 9),
                parameterBuilder.GetMarker("ShipCity", 10),
                parameterBuilder.GetMarker("ShipRegion", 11),
                parameterBuilder.GetMarker("ShipPostalCode", 12),
                parameterBuilder.GetMarker("ShipCountry", 13),
                parameterBuilder.GetMarker("OrderID", 14)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CustomerID", 1);
            p.DbType=DbType.String;
            p.Value=(object)data.CustomerID ?? (object)DBNull.Value;
            p.Size=5;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("EmployeeID", 2);
            p.DbType=DbType.Int32;
            p.Value=data.EmployeeID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipVia", 3);
            p.DbType=DbType.Int32;
            p.Value=data.ShipVia;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderDate", 4);
            p.DbType=DbType.Date;
            p.Value=data.OrderDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("RequiredDate", 5);
            p.DbType=DbType.Date;
            p.Value=data.RequiredDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShippedDate", 6);
            p.DbType=DbType.Date;
            p.Value=data.ShippedDate;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Freight", 7);
            p.DbType=DbType.Decimal;
            p.Value=data.Freight;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipName", 8);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipName ?? (object)DBNull.Value;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipAddress", 9);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipAddress ?? (object)DBNull.Value;
            p.Size=60;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipCity", 10);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipCity ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipRegion", 11);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipRegion ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipPostalCode", 12);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipPostalCode ?? (object)DBNull.Value;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipCountry", 13);
            p.DbType=DbType.String;
            p.Value=(object)data.ShipCountry ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("OrderID", 14);
            p.DbType=DbType.Int32;
            p.Value=data.OrderID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;

    }

}
